package org.mock.base;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;

/**
 * 策略 + 泛型，实现规范化
 */
@Slf4j
public class RequestExecutor {

    public static <T, R> String execute(JSONObject request, RequestHandler<T, R> handler) {
        try {
            T dto = handler.parseRequest(request);
            T wrapper = handler.buildQueryWrapper(dto);
            R data = handler.fetchData(wrapper);
            return handler.processResult(data);
        } catch (Exception e) {
            // 统一异常处理
            log.error("处理请求时发生异常", e);
            return "{'error': '系统错误'}";
        }
    }

}
